package com.yunji.news.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunji.news.common.Result;
import com.yunji.news.entity.ScoreLine;
import com.yunji.news.service.ScoreLineService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 分数线管理控制器
 *
 * @author yunji
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/admin/score-lines")
@RequiredArgsConstructor
public class AdminScoreLineController {

    private final ScoreLineService scoreLineService;

    /**
     * 分页查询分数线列表
     */
    @GetMapping
    public Result<Page<ScoreLine>> getScoreLinePage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) Long majorId,
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) String batch,
            @RequestParam(required = false) String subjectType) {
        
        Page<ScoreLine> scoreLinePage = new Page<>(page, size);
        Page<ScoreLine> result = scoreLineService.getScoreLinePage(
                scoreLinePage, schoolId, majorId, year, batch, subjectType);
        return Result.success(result);
    }

    /**
     * 获取分数线详情
     */
    @GetMapping("/{id}")
    public Result<ScoreLine> getScoreLineDetail(@PathVariable Long id) {
        ScoreLine scoreLine = scoreLineService.getById(id);
        if (scoreLine == null) {
            return Result.error("分数线数据不存在");
        }
        return Result.success(scoreLine);
    }

    /**
     * 根据院校ID获取分数线列表
     */
    @GetMapping("/school/{schoolId}")
    public Result<List<ScoreLine>> getScoreLinesBySchoolId(@PathVariable Long schoolId) {
        List<ScoreLine> scoreLines = scoreLineService.getScoreLinesBySchoolId(schoolId);
        return Result.success(scoreLines);
    }

    /**
     * 根据专业ID获取分数线列表
     */
    @GetMapping("/major/{majorId}")
    public Result<List<ScoreLine>> getScoreLinesByMajorId(@PathVariable Long majorId) {
        List<ScoreLine> scoreLines = scoreLineService.getScoreLinesByMajorId(majorId);
        return Result.success(scoreLines);
    }

    /**
     * 获取最近几年的分数线数据
     */
    @GetMapping("/recent/{years}")
    public Result<List<ScoreLine>> getRecentScoreLines(@PathVariable Integer years) {
        List<ScoreLine> scoreLines = scoreLineService.getRecentScoreLines(years);
        return Result.success(scoreLines);
    }

    /**
     * 根据年份获取分数线列表
     */
    @GetMapping("/year/{year}")
    public Result<List<ScoreLine>> getScoreLinesByYear(@PathVariable Integer year) {
        List<ScoreLine> scoreLines = scoreLineService.getScoreLinesByYear(year);
        return Result.success(scoreLines);
    }

    /**
     * 获取所有年份
     */
    @GetMapping("/years")
    public Result<List<Integer>> getYears() {
        List<Integer> years = scoreLineService.getYears();
        return Result.success(years);
    }

    /**
     * 获取所有批次
     */
    @GetMapping("/batches")
    public Result<List<String>> getBatches() {
        List<String> batches = scoreLineService.getBatches();
        return Result.success(batches);
    }

    /**
     * 获取所有科目类型
     */
    @GetMapping("/subject-types")
    public Result<List<String>> getSubjectTypes() {
        List<String> subjectTypes = scoreLineService.getSubjectTypes();
        return Result.success(subjectTypes);
    }

    /**
     * 创建分数线
     */
    @PostMapping
    public Result<String> createScoreLine(@Valid @RequestBody ScoreLine scoreLine) {
        boolean success = scoreLineService.createScoreLine(scoreLine);
        if (success) {
            return Result.success("创建成功");
        }
        return Result.error("创建失败");
    }

    /**
     * 更新分数线
     */
    @PutMapping("/{id}")
    public Result<String> updateScoreLine(@PathVariable Long id, @Valid @RequestBody ScoreLine scoreLine) {
        scoreLine.setId(id);
        boolean success = scoreLineService.updateScoreLine(scoreLine);
        if (success) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    /**
     * 删除分数线
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteScoreLine(@PathVariable Long id) {
        boolean success = scoreLineService.deleteScoreLine(id);
        if (success) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * 批量删除分数线
     */
    @DeleteMapping("/batch")
    public Result<String> deleteScoreLineBatch(@RequestBody List<Long> ids) {
        boolean success = scoreLineService.deleteScoreLineBatch(ids);
        if (success) {
            return Result.success("批量删除成功");
        }
        return Result.error("批量删除失败");
    }

    /**
     * 批量导入分数线数据
     */
    @PostMapping("/batch-import")
    public Result<String> batchImportScoreLines(@Valid @RequestBody List<ScoreLine> scoreLines) {
        try {
            boolean success = scoreLineService.batchImportScoreLines(scoreLines);
            if (success) {
                return Result.success("批量导入成功");
            }
            return Result.error("批量导入失败");
        } catch (Exception e) {
            return Result.error("批量导入失败：" + e.getMessage());
        }
    }
}